package com.youhome.mobile.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.mobile.utils.JwtTokenUtil;
import com.youhome.service.model.BClient;
import com.youhome.service.model.BCounselor;
import com.youhome.service.model.BShareGroup;
import com.youhome.service.service.BShareGroupHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chen365 on 2018/09/18.
 */
@Api(tags = "分享组房源")
@RestController
@RequestMapping(value = "/share_groups")
public class ShareGroupHouseController {
    @Autowired
    private BShareGroupHouseService shareGroupHouseService;

    @ApiOperation(value = "列表", notes = "列表", response = BShareGroup.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组Id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query")
    })
    @GetMapping(value = "/{shareId}/houses")
    public Object list(HttpServletRequest request, @PathVariable String shareId, Integer pageNumber, Integer pageSize) throws CustomException {
        String phone = new JwtTokenUtil().getUsernameFromToken(request);
        return new ResultPage<>(ResultCode.SUCCESS, shareGroupHouseService.selectByBean(phone, shareId, pageNumber, pageSize));
    }

    @ApiOperation(value = "新增", notes = "新增", response = BClient.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组Id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源Id", required = true, paramType = "query")

    })
    @PostMapping(value = "/{shareId}/houses")
    public Object add(HttpServletRequest request, @PathVariable String shareId, String houseId) throws CustomException {
        String phone = new JwtTokenUtil().getUsernameFromToken(request);
        shareGroupHouseService.insert(phone, shareId, houseId);
        return new Result<>(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "删除", notes = "删除", response = BCounselor.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "shareId", value = "分享组Id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "Long", name = "id", value = "id", required = true, paramType = "path"),
    })
    @DeleteMapping(value = "/{shareId}/houses/{id}")
    public Object delete(HttpServletRequest request, @PathVariable String shareId, @PathVariable Long id) throws CustomException {
        String phone = new JwtTokenUtil().getUsernameFromToken(request);
        shareGroupHouseService.delete(phone, shareId, id);
        return new Result<>(ResultCode.SUCCESS);
    }
}
